$.fn.vld = new Object;
$.fn.vld.pie = new Object;
$.fn.vld.pie.fromPage = function(options) {
	var store = $().rdf();
	$(this).vld.pie.fromStore(options, store);
}

$.fn.vld.pie.fromStore = function(options, store) {
	var div = $(this);
	var r = Raphael("pie_chart");
	r.g.txtattr.font = "12px 'Fontin Sans', Fontin-Sans, sans-serif";
	r.g.text(320, 50, options.title).attr({"font-size": 40});

	var data = [], key = [];
	store.where (
		'?thing <' + options.property + '> ?object'
	).optional (
		'?object <http://www.w3.org/2000/01/rdf-schema#label> ?label'
	).optional (
		'?object <http://xmlns.com/foaf/0.1/> ?name'
	).each (
		function() {
			if (options.ignoreObjects.indexOf(this.object.value._string) >= 0) { return; }
			var objectKey = this.object.value._string;
			if (typeof this.name !== "undefined") { objectKey = this.name.value; }
			if (typeof this.label !== "undefined") { objectKey = this.label.value; }
			var index = key.indexOf(objectKey);
			if (index == -1) {
				data.push(1);
				key.push(objectKey);
			} else {
				data[index] += 1;
			}
		}
	);
	var pie = r.g.piechart( options.centerX, options.centerY, options.radius, data, { legend: key, legendpos: options.legendPos } );
	pie.hover(function () {
		this.sector.stop();
		this.sector.scale(1.1, 1.1, this.cx, this.cy);
		if (this.label) {
			this.label[0].stop();
			this.label[0].scale(1.5);
			this.label[1].attr({"font-weight": 800});
		}
	}, function () {
		this.sector.animate({scale: [1, 1, this.cx, this.cy]}, 500, "bounce");
		if (this.label) {
			this.label[0].animate({scale: 1}, 500, "bounce");
			this.label[1].attr({"font-weight": 400});
		}
	});
}
